package com.dd.cloud.user.service.classroom.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.ClassroomConstants;
import com.dd.cloud.user.entity.classroom.MediaClassify;
import com.dd.cloud.user.enums.ExceptionEnum;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.mapper.classroom.MediaClassifyMapper;
import com.dd.cloud.user.req.classroom.CreateMediaClassifyReq;
import com.dd.cloud.user.req.classroom.QueryMediaClassifyReq;
import com.dd.cloud.user.req.classroom.UpdateMediaClassifyReq;
import com.dd.cloud.user.res.classroom.MediaClassifyRes;
import com.dd.cloud.user.service.classroom.IMediaClassifyService;
import com.dd.cloud.user.service.classroom.IMediaContentService;
import com.dd.cloud.user.service.classroom.IMediaSectionService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 媒体类型表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-10-08
 */
@Service
public class MediaClassifyServiceImpl extends ServiceImpl<MediaClassifyMapper, MediaClassify> implements IMediaClassifyService {
    @Autowired
    private IMediaSectionService mediaSectionService;
    @Autowired
    private IMediaContentService mediaContentService;

    /**
     * 获取云课堂类型列表
     * @return
     */
    @Override
    public List<MediaClassify> getMediaClassifyList() {
        QueryWrapper<MediaClassify> qw = new QueryWrapper<>();
        qw.lambda().eq(MediaClassify::getStatus, ClassroomConstants.ClassifyStatus.NORMAL)
                .select(MediaClassify::getClassify, MediaClassify::getId);
        List<MediaClassify> list = list(qw);
        return list;
    }

    /**
     * 分页获取云课堂类型
     * @param req
     * @return
     */
    @Override
    public Page<MediaClassifyRes> getMediaClassifyPage(QueryMediaClassifyReq req) {
        Page<MediaClassifyRes> page = req.pagePojo();
        if(!StringUtils.isBlank(req.getStatus())){
            req.setStatusStr(req.getStatus().split(","));
        }
        page = getBaseMapper().getClassifyPage(page, req);
        return page;
    }

    /**
     * 确定类型是否存在
     * @param classify 类型
     * @return
     */
    @Override
    public boolean checkClassifyExists(String classify) {
        QueryWrapper<MediaClassify> qw = new QueryWrapper<>();
        qw.lambda().eq(MediaClassify::getStatus, ClassroomConstants.ClassifyStatus.NORMAL)
                .eq(MediaClassify::getClassify, classify);
        MediaClassify mediaClassify = getOne(qw);
        if (mediaClassify == null || mediaClassify.getId() == null){
            return false;
        }
        return true;
    }

    /**
     * 查询不存在的视频类型
     * @param classifyList 要查询的类型数组
     * @return
     */
    @Override
    public List<String> checkClassifyExists(List<String> classifyList) {
        QueryWrapper<MediaClassify> qw = new QueryWrapper<>();
        qw.lambda().eq(MediaClassify::getStatus, ClassroomConstants.ClassifyStatus.NORMAL)
                .in(MediaClassify::getClassify, classifyList);
        List<MediaClassify> mediaClassify = list(qw);
        if (mediaClassify.isEmpty()){
            return classifyList;
        }
        List<String> existsList = mediaClassify.stream().map(MediaClassify::getClassify).collect(Collectors.toList());
        classifyList.removeAll(existsList);
        return classifyList;
    }

    /**
     * 确定类型id是否存在
     * @param id 类型id
     * @return
     */
    @Override
    public boolean checkClassifyExists(Integer id) {
        MediaClassify mediaClassify = getById(id);
        if (mediaClassify == null || mediaClassify.getId() == null){
            return false;
        }
        return true;
    }


    /**
     * 添加类型
     * @param req
     */
    @Override
    public void addMediaClassify(CreateMediaClassifyReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        if (checkClassifyExists(req.getClassify())){
            throw new ZekeException(ExceptionEnum.MEDIA_CLASSIFY_IS_EXISTS);
        }
        MediaClassify classify = new MediaClassify();
        classify.setClassify(req.getClassify());
        classify.setRemarks(req.getRemarks());
        classify.setStatus(ClassroomConstants.ClassifyStatus.NORMAL);
        classify.setCreateDate(LocalDateTime.now());
        classify.setCreateId(loginInfo.getId());
        boolean flag = save(classify);
        if (!flag){
            throw new ZekeException(ZkExceptionEnum.ADD_ERROR);
        }
    }

    /**
     * 修改类型
     * @param req
     */
    @Override
    public void updateMediaClassify(UpdateMediaClassifyReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        MediaClassify classify = getById(req.getId());
        if (classify == null){
            throw new ZekeException(400, "课堂类型id不存在");
        }
        //未修改内容
        if (classify.getClassify().equals(req.getClassify()) && classify.getRemarks().equals(req.getRemarks())){
            return;
        }
        //修改了类型
        if (!classify.getClassify().equals(req.getClassify())){
            if (checkClassifyExists(req.getClassify())){
                throw new ZekeException(ExceptionEnum.MEDIA_CLASSIFY_IS_EXISTS);
            }
        }
        classify.setClassify(req.getClassify());
        classify.setRemarks(req.getRemarks());
        boolean flag = updateById(classify);
        if (!flag){
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }

    /**
     * 修改类型状态
     * @param id
     * @param status
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMediaClassifyStatus(Integer id, Integer status) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        MediaClassify mediaClassify = getById(id);
        if (mediaClassify == null || mediaClassify.getStatus() == ClassroomConstants.ClassifyStatus.DEL){
            throw new ZekeException(400, "类型不存在");
        }
        if (!ObjectUtils.refrect(ClassroomConstants.ClassifyStatus.class, status)) {
            throw new ZekeException(ZkExceptionEnum.STATUS_DATA_ERROR);
        }
        if (mediaClassify.getStatus().equals(status)){
            return;
        }
        mediaClassify.setStatus(status);
        boolean flag = updateById(mediaClassify);
        if (!flag){
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
        if (status == ClassroomConstants.ClassifyStatus.HIDDEN){
            status = ClassroomConstants.SectionStatus.RELETED;
        }
        mediaSectionService.updateSectionStatusByClassify(id, status);
        mediaContentService.updateContentStatusByClassify(id, status);
    }
}
